﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RED_NET_Core.BLL;
using RED_NET_Core.Entity;
using RED_NET_Core.Utilty;
namespace RED_NET.ContentPage
{
    public partial class MessageBoard : System.Web.UI.Page
    {
        private static int  visitId;
        private static Users user;
        protected void Init()
        {
            Session["PageName"] = "MessageBoard";
            user = new Users();
            user.UserId = -1;
            if (Session["USER"] != null)
                user = (Users)Session["USER"];
            else
                DivLeaveMessage.Visible = false;
            if(Request.QueryString["VISIT_ID"]!=null)
            visitId = Int32.Parse(Request.QueryString["VISIT_ID"]);
            else
                 Response.Redirect("~/ContentPage/Error/PageError.aspx");
         
            DataTable _dt = new DataTable();
            try
            {
                 _dt = News_BLL.ShowMessageList(visitId);
                GridView_Message.DataSource = _dt;
            GridView_Message.DataBind();
            }
            catch (Exception ex)
            {
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
          
        }
        protected void GridView_Message_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_Message.PageIndex = e.NewPageIndex;
            Init(); //重新绑定GridView数据的函数
            GridView_Message.PageIndex = e.NewPageIndex;

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                Init();
            }

        }
        protected void GridView_Message_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {




                e.Row.Cells[1].Visible = false; //如果想使第1列不可见,则将它的可见性设为false
            }
        }
        protected void GridView_Message_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton LinkButton_Reply = (LinkButton)e.Row.FindControl("LinkButton_Reply");

               LinkButton_Reply.PostBackUrl = "ShowDaily.aspx?_Id=" + e.Row.Cells[1].Text.ToString() + "&_IsMessage=1&VISIT_ID=" + Request.QueryString["VISIT_ID"];

                LinkButton LinkButton_Delete = (LinkButton)e.Row.FindControl("LinkButton_Delete");
                LinkButton_Delete.CommandArgument = e.Row.RowIndex.ToString();

                if (Session["USER"] == null)
                {
                  //  Panel Panel_Edit = (Panel)e.Row.FindControl("Panel_Edit");
                 //   Panel_Edit.Visible = false;
                    LinkButton_Reply.Text = "查看";
                    LinkButton_Delete.Visible = false;
                }
                else if (user.UserId!= visitId&&user.Right!=3)
                {
                    LinkButton_Delete.Visible = false;
                }
            }
        }

        protected void GridView_Message_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {

                int _index = Convert.ToInt32(e.CommandArgument);
                int _Id = Int32.Parse(GridView_Message.Rows[_index].Cells[1].Text);
                try
                {
                    News_BLL.DeleteMessage(_Id);
                }
                catch (Exception ex)
                {
                    Response.Redirect("~/ContentPage/Error/PageError.aspx");
                } 
                News _news=News_BLL.ShowNews(_Id);
                if (user.Right == 3&&_news.UId!=user.UserId)
                {

                    string title = "留言删除通知";
                    string _content = "很抱歉，您在\""+user.NickName+"\"博客的留言由于涉及违规内容已被删除！";
                    int _num = 1;
                    int[] _sendto = new int[10];
                    _sendto[0] = visitId;
                    LoginBLL _login = new LoginBLL();
                    Users _user = _login.GetMessageOfUser(_news.UId);
                    string _sendtoStr = _user.NickName + ";";
                    News_BLL.AddLetter(user.UserId, _content, title, _sendto, _num, _sendtoStr);
                }
                Response.Redirect(Request.Url.ToString());
            }

        }

        protected void DataList_Message_ItemDataBound(object sender, EventArgs e)
        {

        }
        protected void LinkButton_Public_Click(object sender, EventArgs e)
        {
            string _content = TextBox_Content.Text;
            _content = this.Server.HtmlEncode(_content).Replace("\n", " <br/>").Replace(" ", " ");//用HTML换行符替代Textbox中的换行符
            _content = this.Server.HtmlDecode(_content);
            ReturnValue ret = Safety.JudgeStr(_content, 4);
            if (ret.value == 0)
            {
                try{
                News_BLL.AddMessage(user.UserId, _content, visitId);
                }
                catch (Exception ex)
                {
                    Response.Redirect("~/ContentPage/Error/PageError.aspx");
                }
                TextBox_Content.Text = "";
                Init();
            }
            else
            {
                Label_Comment_Error.Text = ret.error;
            }
        }

    }
}
